from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
import unittest

import sys
sys.path.append('..')
from d_tool.get_csv_data import get_csv_data
from d_tool.insert_img import insert_img
from web_base.base_page import BasePage
from test_case.options import options

class DiscuzPage(BasePage):
    url = r'http://192.168.126.131/upload/'
    username_loc = (By.ID, 'ls_username')
    password_loc = (By.ID, 'ls_password')
    submit_loc = (By.XPATH, '//form[@id="lsform"]/div/div/table/tbody/tr[2]/td[3]/button')

    #退出登录定位
    logout_loc = (By.LINK_TEXT,'退出')
    #默认板块
    blank_loc=(By.LINK_TEXT,'默认版块')
    #帖子定位
    tz_loc=(By.XPATH,'//*[@id="moderate"]/table/tbody[4]/tr/th/a[2]')
    # 发帖
    newtid_btn_loc = (By.XPATH, '//*[@id="newspecial"]/img')  # 点击发帖
    title_loc = (By.ID, 'subject')
    iframe_loc = (By.ID, 'ajaxpostframe')
    kw_loc = (By.XPATH, '/html/body')
    submit_btn_loc = (By.ID, 'postsubmit')

    # 回帖
    reply_btn_loc = (By.XPATH, '//*[@id="post_reply"]/img')
    reply_kw_loc = (By.ID, 'postmessage')
    reply_submit_btn_loc = (By.ID, 'postsubmit')
    def __init__(self,driver):
        super().__init__(driver)#将driver传给父类
        self.get_url(self.url)
    #登录
    def login(self,username,password):
        self.ele_clear(self.username_loc)
        self.input_ele(self.username_loc,username)
        self.ele_clear(self.password_loc)
        self.input_ele(self.password_loc,password)
        self.ele_click(self.submit_loc)
        sleep(3)

    #看贴
    def read(self):
        self.ele_click(self.blank_loc)
        sleep(3)
        self.ele_click(self.tz_loc)
        sleep(3)
    # 注册
    def register(self,newname,pwd,pwd2,email):
        self.ele_click(self.register_btn_loc)
        sleep(2)
        self.input_ele(self.newname_loc, newname)
        sleep(2)
        self.input_ele(self.pwd_loc, pwd)
        sleep(1)
        self.input_ele(self.pwd2_loc, pwd2)
        sleep(1)
        self.input_ele(self.email_loc, email)
        sleep(2)
        self.ele_click(self.register_submit_btn_loc)
        sleep(5)
    #发帖
    def post(self,username,password,title,kw):
        self.login(username, password)  # 登录
        self.ele_click(self.fid_btn_loc)  # 点击默认版块
        sleep(1)
        self.ele_click(self.newtid_btn_loc)  # 点击发新帖
        self.input_ele(self.title_loc, title)  # 输入标题
        sleep(2)
        self.switchto_ele(self.iframe_loc)  # 跳转到iframe框架
        self.input_ele(self.kw_loc, kw)  # 输入帖子内容
        sleep(2)
        self.switchout_ele()  # 跳出iframe框架
        self.ele_click(self.submit_btn_loc)  # 点击发布新帖
        sleep(5)
    #回帖
    def reply(self, username, password, reply_kw):
        self.login(username, password)  # 登录
        self.read()
        sleep(1)
        self.ele_click(self.reply_btn_loc)
        sleep(1)
        self.input_ele(self.reply_kw_loc, reply_kw)
        sleep(1)
        self.ele_click(self.reply_submit_btn_loc)  # 提交回帖内容
        sleep(2)

    #退出
    def logout(self):
        self.ele_click(self.logout_loc)
        sleep(3)

if __name__ == '__main__':
    username = 't01'
    password = '123456'


    driver = webdriver.Chrome(options=options)
    dicsuz_page = DiscuzPage(driver)


    #测试登录
    # dicsuz_page.assert(username,password)
    #测试退出
    # dicsuz_page.logout()
    #测试看帖
    dicsuz_page.read()



